// 配置路由相关信息
import VueRouter from "vue-router"
import Vue from "vue"
import solution1 from "../views/first-solution.vue"
import solution2 from "../views/second-solution.vue"
import solution3 from "../views/third-solution.vue"
import login from "../views/login.vue"
import zidingyi from "../views/zidingyi.vue"
import count from "../views/count.vue"
import routerPractise from "../views/router-practise.vue"
import home from "../views/subNav/home.vue"
import category from "../views/subNav/category.vue"
import about from "../views/subNav/about.vue"
import detail from "../views/subNav/details.vue"
// 1.通过Vue.use(插件)
Vue.use(VueRouter);
// 解决重复点击路由报错的BUG
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}
const routes = [
  {
    path: '/first-solution',
    component: solution1
  },
  {
    path: '/second-solution',
    component: solution2
  },
  {
    path: '/third-solution',
    component: solution3
  },
  {
    path: '/zidingyi',
    component: zidingyi
  },
  {
    path: '/count',
    component: count
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/router-practise',
    component: routerPractise,
    children: [
      {
        // 这里的嵌套路由千万不要在前面加斜杠 /
        path: 'home',
        meta: {title: '主页'},
        component: home,
        children: [
          {
            name: 'my-detail',
            path: 'detail',
            component: detail,
            meta: {isAuth: true, title: '详细信息'}
          },
        ]
      },
      {
        path: 'about',
        component: about,
        meta: {title: '关于'}
      },
      {
        path: 'category',
        component: category
      },
    ]
  },
];
// 2.创建VueRouter对象
const router = new VueRouter({
  routes,
});
// 全局前置路由守卫——初始化的时候被调用
router.beforeEach((to, from, next) => {
      // 有优化写法
//   if(to.name === 'xinwen' || to.path === 'home/message') {
  console.log(document.title);
  document.title = to.meta.title || 'Vue';
  if(to.meta.isAuth) {
    if(true) {
      alert('通过')
      next();
    } else {
      alert('无权限查看')
    }
  }
  else{
    next();
  }
})
// 后置路由守卫

// 3.将router传入到vue实例中
export default router;
